import xlrd, MuConnection, MuNetworkConfiguration, sys, getopt


USERNAME = 'admin'
PASSWORD = ''
MU = ''
WORKBOOK = ''

def usage():
	print "python runExcelTest.py -i https://demo.myMu.dom -u admin -p myPassword -w /path/myWorkbook.xls"
	print "-i = url or ipadress of the Mu plaftorm"
	print "-u = username, only admin can run API"
	print "-p = password for admin"
	print "-w = full path and name for USGv6 workbook"
	sys.exit(2)

def writeResults(output):
	data = ""
	newFile = open(output, "wt")
	for result in RESULTS:
		testName = result[0]
		testType = result[1]
		results = result[2]
		for singleResult in results:
			if not singleResult.testResult=="END":
				data = '"'+testName+'","'+testType+'",'+singleResult.getAsCsv()
				newFile.write(data)
	newFile.close()
		

#get the username and password
args = sys.argv[0:]
try:
    options, remainder = getopt.getopt(sys.argv[1:], "u:p:i:w:o:", ["username=", "password=","ipaddress=", "workbook=", "output="])
except getopt.GetoptError, err:
    # print help information and exit:
    print str(err) # will print something like "option -a not recognized"
    usage()
    sys.exit(2)
for o, p in options:
    if o in ("-u","--username"):
		USERNAME = p
    elif o in ("-p", "--password"):
		PASSWORD = p
    elif o in ("-i", "--ipaddress"):
		MU = p
    elif o in ("-w", "--workbook"):
    	WORKBOOK = p
    else:
    	usage()
	
if MU=='':
	usage()
if PASSWORD=='':
	usage()
if WORKBOOK=='':
	usage()
	
book = xlrd.open_workbook(WORKBOOK)
myMuConnection = MuConnection.MuConnection(MU,USERNAME,PASSWORD)
myNetConfigApi = MuNetworkConfiguration.MuNetworkConfig(myMuConnection)

# lets find the test sheet and set up the test
hostConfigData = book.sheet_by_name("Hosts")
print hostConfigData.name, hostConfigData.nrows, hostConfigData.ncols
skip = True
for rx in range(hostConfigData.nrows):
	print rx
	if skip:
		skip = False
		continue
	row = hostConfigData.row(rx)
	if row[0] == None or row[0] == '':
		print "empty row"
	else:
		host = MuNetworkConfiguration.host()
		host.setName(row[0].value)
		host.setV4_addr(row[1].value)
		host.setV6_local_addr(row[2].value)
		host.setV6_global_addr(row[3].value)
		host.setMac(row[4].value)
		host.setNotes(row[5].value)
		print host
		response = myNetConfigApi.addHost(host)
		if response.hasError:
			response = myNetConfigApi.modifyHost(host)
		print response
		
	